package crawlerv2;

/**
 *
 * @author Samer Al-Kiswany
 * modified by: Miguel Guanlao
 */
public class CrawlResult {
    protected String ultrapeers;
    protected String leaves;
    protected int numOfFiles;
    protected String filesList;
    protected String Agent;
    //IPAddress and HostName are added
    protected String IPAddress;
    protected String HostName;
    
    /** Creates a new instance of CrawlResults */
    public CrawlResult() {
        ultrapeers=new String();
        leaves=new String();
        numOfFiles=0;
        filesList=new String(); 
        Agent=new String();
        IPAddress=new String();
        HostName=new String();
    }
    
    public void setIPAddress(String ipaddr){
    	IPAddress=ipaddr;
    }
    
    public String getIPAddress(){
        return IPAddress;
    }
    
    public void setHostName(String host){
    	HostName=host;
    }
    
    public String getHostName(){
        return HostName;
    }
    
    public void setUltrapeers(String upeers){
        ultrapeers=upeers;
    }
    public String getUltrapeers(){
        return ultrapeers;
    }
    
    public void setLeaves(String lvs){
        leaves=lvs;
    }
    public String getLeaves(){
        return leaves;
    }
    
    public void addNumOfFiles(int num){
        numOfFiles+=num;
    }
    public int getNumOfFiles(){
        return numOfFiles;
    }
    
    public void addFilesList(String flist){
        filesList+=flist;
    }
    public String getFilesList(){
        return filesList;
    }
    public void setAgent(String agnt){
        Agent=agnt;
    }
    public String getAgent(){
        return Agent;
    }
    public void print(){
        System.out.println("Running Aggent : "+getAgent());
        System.out.println("UltraPeers : "+getUltrapeers());
        System.out.println("Leaves : "+getLeaves());
        System.out.println("Number of files: "+getNumOfFiles());
        if(getNumOfFiles()>0)
            System.out.println("list of files : "+getFilesList().replace("\0"," : "));
    }
    
    public String toString(){
        String result=new String();
        result=getAgent()+"\n";
        result+=getUltrapeers()+"\n";
        result+=getLeaves()+"\n";
        result+=getNumOfFiles()+"\n";
        result+=getFilesList();
        
        return result;
    }
    
    public void parseResult(String result){
        int beginIndex;
        int endIndex;
        String str=new String();
                
        endIndex=result.indexOf("\n");
        str=result.substring(0,endIndex);
        setUltrapeers(str);
        
        beginIndex=endIndex;        
        endIndex=result.indexOf("\n");
        str=result.substring(beginIndex,endIndex);
        setLeaves(str);       
    
        beginIndex=endIndex;        
        endIndex=result.indexOf("\n");
        str=result.substring(beginIndex,endIndex);
        addNumOfFiles(Integer.parseInt(str)); 
        
        beginIndex=endIndex;        
        endIndex=result.indexOf("\n");
        str=result.substring(beginIndex,endIndex);
        addFilesList(str); 
        
    }
}
